package com.ruoyi.resume.mapper;

import com.ruoyi.resume.domain.model.resumeProfile.*;
import com.ruoyi.resume.provider.ResumeSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ResumeMapper {
    @InsertProvider(type = ResumeSqlProvider.class, method = "insertResume")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertResume(BasicInfo basicInfo);

    @InsertProvider(type = ResumeSqlProvider.class, method = "insertEducations")
    int insertEducations(@Param("resumeId") Integer resumeId,
                         @Param("educations") List<EducationBackground> educations);

    @InsertProvider(type = ResumeSqlProvider.class, method = "insertInternships")
    int insertInternships(@Param("resumeId") Integer resumeId,
                          @Param("internships") List<InternshipExperience> internships);

    @InsertProvider(type = ResumeSqlProvider.class, method = "insertWorks")
    int insertWorks(@Param("resumeId") Integer resumeId,
                    @Param("works") List<WorkExperience> works);

    @Insert("INSERT INTO collect_resume (user_id, resume_id) VALUES (#{userId}, #{resumeId})")
    @Results({
            @Result(column = "user_id", property = "userId"),
            @Result(column = "resume_id", property = "resumeId")
    })
    int insertCollectedResume(@Param("userId") Long userId, @Param("resumeId") Integer resumeId);

    @Select("SELECT resume_id FROM collect_resume WHERE user_id = #{userId}")
    @Results({
            @Result(column = "resume_id", property = "resumeId")
    })
    List<Integer> selectCollectedResumesByUserId(Long userId);

    /**
     * 根据简历id查询主表信息
     * */
    @Select("SELECT *  FROM resume WHERE id = #{resumeId}")
    BasicInfo selectResumeById(Integer resumeId);


    /**
     * 根据条件查询主表信息
     * */
    @Select("<script>" +
            "SELECT id, user_id, name, gender, birthdate, phone, email, school, nation, degree, residence, " +
            "political_status, age, created_at, updated_at, is_outer, status, skills " +
            "FROM resume " +
            "WHERE id=#{resumeId} " +
            "<if test='name != null'>AND name LIKE CONCAT('%',#{name},'%')</if> " +
            "<if test='gender != null'>AND gender = #{gender}</if> " +
            "<if test='nativePlace != null'>AND residence LIKE CONCAT(#{nativePlace},'%')</if> " +
            "<if test='politicalStatus != null'>AND political_status = #{politicalStatus}</if> " +
            "ORDER BY id ASC " +
            "</script>")
    BasicInfo selectResumesByConditions(
            @Param("resumeId") Integer resumeId,
            @Param("name") String name,
            @Param("gender") String gender,
            @Param("nativePlace") String nativePlace,
            @Param("politicalStatus") String politicalStatus);

    @Delete("DELETE FROM collect_resume WHERE user_id = #{userId} AND resume_id = #{resumeId}")
    void deleteCollectedResume(@Param("userId") Long userId, @Param("resumeId") Integer resumeId);

    @Update("UPDATE resume SET status = #{status} WHERE id = #{resumeId}")
    void updateResumeStatus(@Param("resumeId") int resumeId, @Param("status") int status);

    @InsertProvider(type = ResumeSqlProvider.class, method = "insertProjects")
    int insertProjects(@Param("resumeId") Integer resumeId,
                       @Param("projects") List<ProjectExperience> projects);

    @Select("<script>" +
            "SELECT id, user_id, name, gender, birthdate, phone, email, school, nation, degree, residence, " +
            "political_status, age, created_at, updated_at, is_outer, status, skills " +
            "FROM resume " +
            "WHERE 1=1 " +
            "<if test='isOuter != null'>AND is_outer = #{isOuter}</if> " +
            "<if test='status != null'>AND status = #{status}</if> " +
            "<if test='name != null'>AND name LIKE CONCAT('%',#{name},'%')</if> " +
            "<if test='gender != null'>AND gender = #{gender}</if> " +
            "<if test='nativePlace != null'>AND residence LIKE CONCAT(#{nativePlace},'%')</if> " +
            "<if test='politicalStatus != null'>AND political_status = #{politicalStatus}</if> " +
            "ORDER BY id ASC " +
            "</script>")
            //"LIMIT #{start}, #{count}" +

    List<BasicInfo> selectResumesByDynamicFilter(//@Param("start") int start,
                                                 //@Param("count") int count,
                                                 @Param("name") String name,
                                                 @Param("gender") String gender,
                                                 @Param("nativePlace") String nativePlace,
                                                 @Param("politicalStatus") String politicalStatus,
                                                 @Param("isOuter") Integer isOuter,
                                                 @Param("status") Integer status);

    @Select("SELECT name FROM resume WHERE id = #{resumeId}")
    String findResumeNameById(@Param("resumeId") Integer resumeId);

    @Delete("DELETE FROM resume WHERE id = #{resumeId}")
    void deleteResumeById(int resumeId);

    @Update("UPDATE resume SET user_id = #{userId}, name = #{name}, gender = #{gender}, birthdate = #{birthdate}, phone = #{phone}, email = #{email}, school = #{school}, nation = #{nation}, degree = #{degree}, residence = #{residence}, political_status = #{politicalStatus}, age = #{age}, skills = #{skills}, is_outer = #{isOuter}, status = #{status} WHERE id = #{id}")
    void updateResume(BasicInfo basicInfo);


    @Select("SELECT id, user_id, name, gender, birthdate, phone, email, school, nation, degree, residence, " +
            "political_status, age, created_at, updated_at, is_outer, status, skills " +
            "FROM resume WHERE id = #{resumeId}")
    BasicInfo selectResumeBasicInfoById(@Param("resumeId") int resumeId);

    @Select("SELECT * FROM resume order by id desc limit 10")
    List<BasicInfo> selectLatestResumes();

    /**
     * 检查是否有重复简历
     */
    @Select("<script>" +
            "SELECT id FROM resume " +
            "WHERE name = #{name} " +
            "AND (" +
            "  phone = #{phone} " +
            "  OR email = #{email} " +
            ") " +
            "ORDER BY id DESC" +
            "</script>")
    List<Integer> selectDuplicateResumeIds(@Param("name") String name,
                                           @Param("phone") String phone,
                                           @Param("email") String email);
}